//
//  PurchaseOrderContentViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/12.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "PurchaseOrderContentViewController.h"
#import "PurchaseOrderTableViewCell.h"
#import "PurchaseOrderDetailViewController.h"

#define kSelectedViewHeight jisuan(95)

static NSString *goodsCellID = @"goodsCellID";

@interface PurchaseOrderContentViewController ()<UITableViewDelegate, UITableViewDataSource>


@property (strong, nonatomic) UIView *selectedView;
@property (strong, nonatomic) NSMutableArray *array;
@property (strong, nonatomic) NSMutableArray *selectorPatnArray;

@property (strong, nonatomic) UIImageView *lineupIV;
@property (strong, nonatomic) UIImageView *redemptionIV;

@property (strong, nonatomic) UIButton *allSelectedBtn;

@end

@implementation PurchaseOrderContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WhiteColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeBottom;
    
    self.array = [NSMutableArray array];
    for (int i = 0; i < 10; i++) {
        
        NSString *str = [NSString stringWithFormat:@"第%d行",i + 1];
        [self.array addObject:str];
    }
    
    self.selectorPatnArray = [NSMutableArray array];
    
    [self.view addSubview:self.selectedView];
    [self.view addSubview:self.gooodsTableView];
    
    
    self.selectedView.frame = CGRectMake(0, 0, WIDTH, kSelectedViewHeight);
//    self.gooodsTableView.frame = self.view.bounds;
    __weak typeof(self) weakSelf = self;
    [self.gooodsTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.view.mas_top);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom);
    }];

    [self createSelectedView];

}

#pragma mark -全选
- (void)allCellSelected:(UIButton *)btn {
    
    btn.selected = !btn.selected;

    [btn setTitle:@"全选" forState:UIControlStateNormal];
    
    
    if (btn.selected) {
        
        NSLog(@"全选");
        
        for (int i = 0; i< self.array.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            [self.gooodsTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
            
        }
        
        
    }else {
        
        NSLog(@"不全选");
        
        for (int i = 0; i< self.array.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            [self.gooodsTableView deselectRowAtIndexPath:indexPath animated:YES];
            
        }
        
    }
    
}
#pragma mark -申请赎回
- (void)redempGestureClick:(UIGestureRecognizer *)recognizer {
    
    NSLog(@"申请赎回");
//    self.redemptionIV.image = [UIImage imageNamed:@"lineUpSelected"];
//    self.lineupIV.image = [UIImage imageNamed:@"lineUpNotSelected"];
    
}

#pragma mark -继续排队
- (void)lineupGestureClick:(UIGestureRecognizer *)recognizer {
    
    NSLog(@"继续排队");
//    self.lineupIV.image = [UIImage imageNamed:@"lineUpSelected"];
//    self.redemptionIV.image = [UIImage imageNamed:@"lineUpNotSelected"];
    
}

#pragma mark -是否显示selectedView
- (void)isHiddenSelectedView:(BOOL)isHidden {
    
//    self.selectedView.hidden = isHidden;
    
    self.allSelectedBtn.selected = NO;

    
    if (isHidden) {
        
        NSLog(@"隐藏");
        self.gooodsTableView.frame = self.view.bounds;


        
    }else {

        self.gooodsTableView.allowsMultipleSelectionDuringEditing = YES;
        self.gooodsTableView.editing = YES;

        
        NSLog(@"显示");
        self.gooodsTableView.frame = CGRectMake(0, kSelectedViewHeight, WIDTH, self.view.height - kSelectedViewHeight);
        
    }
    
}

- (void)createSelectedView {
    
    __weak typeof(self) weakSelf = self;
    
    UIImageView *bgV = [[UIImageView alloc] init];
    bgV.contentMode = UIViewContentModeScaleAspectFit;
    bgV.image = [UIImage imageNamed:@"selectedBG"];
    [self.selectedView addSubview:bgV];
    [bgV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.selectedView.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.selectedView.mas_centerY);
        make.width.mas_equalTo(jisuan(722));
        make.height.mas_equalTo(jisuan(92));
        
    }];

    UIButton *allSelectedBtn = [[UIButton alloc] init];
    self.allSelectedBtn = allSelectedBtn;
    [allSelectedBtn setImage:[UIImage imageNamed:@"cellAllNotSelected"] forState:UIControlStateNormal];
    [allSelectedBtn setImage:[UIImage imageNamed:@"allCellSelected"] forState:UIControlStateSelected];
    [allSelectedBtn setTitle:@"全选" forState:UIControlStateSelected];
    [allSelectedBtn setTitle:@"全选" forState:UIControlStateNormal];
    [allSelectedBtn setTitleColor:Color333333 forState:UIControlStateNormal];
    allSelectedBtn.titleLabel.font = Font(20);
    [allSelectedBtn addTarget:self action:@selector(allCellSelected:) forControlEvents:UIControlEventTouchUpInside];
    [self.selectedView addSubview:allSelectedBtn];
    [allSelectedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.selectedView.mas_top);
//        make.centerX.mas_equalTo(weakSelf.selectedView.mas_centerX);
        make.left.mas_equalTo(bgV.mas_left).with.offset(jisuan(8));
        make.width.mas_equalTo(jisuan(120));
        make.height.mas_equalTo(jisuan(92));
        
    }];
    
    allSelectedBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, jisuan(-10));

    
    self.redemptionIV = [[UIImageView alloc] init];
    self.redemptionIV.contentMode = UIViewContentModeScaleAspectFit;
    self.redemptionIV.image = [UIImage imageNamed:@"lineUpNotSelected"];
    self.redemptionIV.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *redempGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(redempGestureClick:)];
    [self.redemptionIV addGestureRecognizer:redempGesture];
    [self.selectedView addSubview:self.redemptionIV];
    [self.redemptionIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.selectedView.mas_right).with.offset(-jisuan(40));
        make.centerY.mas_equalTo(allSelectedBtn.mas_centerY);
        make.width.mas_equalTo(jisuan(120));
        make.height.mas_equalTo(jisuan(40));
        
    }];
    
    UILabel *redemptionL = [[UILabel alloc] init];
    redemptionL.text = @"申请赎回";
    redemptionL.textColor = Color333333;
    redemptionL.font = Font(20);
    redemptionL.textAlignment = NSTextAlignmentCenter;
    [self.selectedView addSubview:redemptionL];
    [redemptionL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(weakSelf.redemptionIV.mas_centerY);
        make.centerX.mas_equalTo(weakSelf.redemptionIV.mas_centerX);
    }];
    
    self.lineupIV = [[UIImageView alloc] init];
    self.lineupIV.contentMode = UIViewContentModeScaleAspectFit;
    self.lineupIV.image = [UIImage imageNamed:@"lineUpSelected"];
    self.lineupIV.userInteractionEnabled = YES;
    [self.selectedView addSubview:self.lineupIV];
    UITapGestureRecognizer *lineupGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lineupGestureClick:)];
    [self.lineupIV addGestureRecognizer:lineupGesture];
    [self.lineupIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.redemptionIV.mas_left).with.offset(-jisuan(30));
        make.centerY.mas_equalTo(weakSelf.redemptionIV.mas_centerY);
        make.width.mas_equalTo(jisuan(120));
        make.height.mas_equalTo(jisuan(40));
        
    }];
    UILabel *lineupL = [[UILabel alloc] init];
    lineupL.text = @"继续排队";
    lineupL.textColor = WhiteColor;
    lineupL.font = Font(20);
    lineupL.textAlignment = NSTextAlignmentCenter;
    [self.selectedView addSubview:lineupL];
    [lineupL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(weakSelf.lineupIV.mas_centerY);
        make.centerX.mas_equalTo(weakSelf.lineupIV.mas_centerX);
    }];
    
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;

}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //选中数据
    [self.selectorPatnArray addObject:self.array[indexPath.row]];
    
    // 编辑模式下不允许点击进入详情
    if (tableView.isEditing) {
        // 去掉选中时的cell背景颜色
//        PurchaseOrderTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//        cell.selectedBackgroundView = [[UIView alloc] init];
//        cell.contentView.backgroundColor = WhiteColor;

        return;
    
    }else {

        PurchaseOrderDetailViewController *detailVC = [[PurchaseOrderDetailViewController alloc] init];
        
        detailVC.tradeType = self.tradeType;
        
        [self.navigationController pushViewController:detailVC animated:YES];
        
    }

}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    //从选中中取消
    if (self.selectorPatnArray.count > 0) {

        [self.selectorPatnArray removeObject:self.array[indexPath.row]];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return jisuan(370);
    
}

#pragma mark -delegate\datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.array.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PurchaseOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:goodsCellID];
    
    if (!cell) {
        
        cell = [[PurchaseOrderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:goodsCellID];
        
    }
    
    
    cell.type = self.tradeType;
    // cell 赋值
    cell.dict = @{@"name":@"1"};
    cell.selectedBackgroundView = [[UIView alloc] init];
    
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    
    return cell;
    
}


#pragma mark -lazy

- (UIView *)selectedView {
    
    if (!_selectedView) {
    
        _selectedView = [[UIView alloc] init];
        _selectedView.backgroundColor = WhiteColor;
    }
    
    return _selectedView;
}

- (UITableView *)gooodsTableView {
    
    if (!_gooodsTableView) {
        
        _gooodsTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        
        _gooodsTableView.delegate = self;
        _gooodsTableView.dataSource = self;
        
        _gooodsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _gooodsTableView.showsVerticalScrollIndicator = NO;
        _gooodsTableView.showsHorizontalScrollIndicator = NO;
        
        _gooodsTableView.estimatedSectionHeaderHeight = 0;
        _gooodsTableView.estimatedSectionFooterHeight = 0;
        _gooodsTableView.estimatedRowHeight = 0;
        
        if (@available(iOS 11.0, *)) {
            
            _gooodsTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        }
        
    }
    
    return _gooodsTableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
